<template>
  <div class="app-container home">
    <el-row :gutter="20">
      <el-col :sm="24" :lg="12" style="padding-left: 20px">
        <h2>若依后台管理框架</h2>
        <p>
          一直想做一款后台管理系统，看了很多优秀的开源项目但是发现没有合适自己的。于是利用空闲休息时间开始自己写一套后台系统。如此有了若依管理系统，她可以用于所有的Web应用程序，如网站管理后台，网站会员中心，CMS，CRM，OA等等，当然，您也可以对她进行深度定制，以做出更强系统。所有前端后台代码封装过后十分精简易上手，出错概率低。同时支持移动客户端访问。系统会陆续更新一些实用功能。
        </p>
        <p>
          <b>当前版本:</b> <span>v{{ version }}</span>
        </p>
        <p>
          <el-tag type="danger">&yen;免费开源</el-tag>
        </p>
        <p>
          <el-button
            type="primary"
            icon="Cloudy"
            plain
            @click="goTarget('https://gitee.com/y_project/RuoYi-Cloud')"
            >访问码云</el-button
          >
          <el-button
            icon="HomeFilled"
            plain
            @click="goTarget('http://ruoyi.vip')"
            >访问主页</el-button
          >
        </p>
      </el-col>

      <el-col :sm="24" :lg="12" style="padding-left: 50px">
        <el-row>
          <el-col :span="12">
            <h2>技术选型</h2>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <h4>后端技术</h4>
            <ul>
              <li>SpringBoot</li>
              <li>SpringCloud</li>
              <li>Nacos</li>
              <li>Sentinel</li>
              <li>Seata</li>
              <li>Minio</li>
              <li>...</li>
            </ul>
          </el-col>
          <el-col :span="6">
            <h4>前端技术</h4>
            <ul>
              <li>Vue</li>
              <li>Vuex</li>
              <li>Element-ui</li>
              <li>Axios</li>
              <li>Echarts</li>
              <li>Quill</li>
              <li>...</li>
            </ul>
          </el-col>
        </el-row>
      </el-col>
    </el-row>
    <el-divider />
    <el-row :gutter="20">
      <el-col :xs="24" :sm="24" :md="12" :lg="8">
        <el-card class="update-log">
          <template v-slot:header>
            <div class="clearfix">
              <span>订单统计</span>
            </div>
          </template>
          <div ref="chartRef" style="width: 100%; height: 400px;"></div>
        </el-card>
      </el-col>
      <el-col :xs="24" :sm="24" :md="12" :lg="8">
        <el-card class="update-log">
          <template v-slot:header>
            <div class="clearfix">
              <span>救援任务统计</span>
            </div>
          </template>
          <div ref="statusChartRef" style="width: 100%; height: 400px;"></div>
        </el-card>
      </el-col>
      <el-col :xs="24" :sm="24" :md="12" :lg="8">
        <el-card class="update-log">
          <template v-slot:header>
            <div class="clearfix">
              <span>捐赠支持</span>
            </div>
          </template>
          <div class="body">
            <img
              src="@/assets/images/pay.png"
              alt="donate"
              style="width:100%"
            />
            <span style="display: inline-block; height: 30px; line-height: 30px"
              >你可以请作者喝杯咖啡表示鼓励</span
            >
          </div>
        </el-card>
      </el-col>
    </el-row>

  </div>
</template>

<script setup name="Index">
import { ref, reactive, onMounted } from "vue";
import * as echarts from "echarts";
import {listOrders} from "../api/masterrabbit-kaixuan/orders.js";
import {listEmergencyrescuerecords} from "../api/masterrabbit-kaixuan/emergencyrescuerecords.js"; // 发送 API 请求

// 绑定 ECharts 容器
const chartRef = ref(null);

// 查询参数
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  serviceCategory: null, // 查询订单类别
});

// **订单类别映射**
const categoryMap = {
  1: "车辆维修订单",
  2: "汽车保养订单",
  3: "专业洗车订单",
  4: "就近加油订单",
};

// **获取订单数据**
const fetchOrders = async () => {
  try {
    listOrders(queryParams).then(response => {
      const orders = response.rows || []; // 确保数据存在
      const categoryStats = processOrderData(orders);
      renderChart(categoryStats);
    });
  } catch (error) {
    console.error("获取订单数据失败:", error);
  }
};

// **统计 `serviceCategory` 数量**
const processOrderData = (orders) => {
  const stats = { 1: 0, 2: 0, 3: 0, 4: 0 }; // 统计 1,2,3,4 类别订单数

  orders.forEach(order => {
    const category = order.serviceCategory;
    if (stats[category] !== undefined) {
      stats[category]++;
    }
  });

  return Object.entries(stats).map(([key, value]) => ({
    name: categoryMap[key], // 转换成实际的订单名称
    value: value
  }));
};

// **渲染 ECharts 饼状图**
const renderChart = (categoryStats) => {
  if (!chartRef.value) return;

  const myChart = echarts.init(chartRef.value);

  const option = {
    title: { text: "订单服务分类占比", left: "center" },
    tooltip: { trigger: "item" },
    legend: { orient: "vertical", left: "left" },
    series: [
      {
        name: "订单数量",
        type: "pie",
        radius: "50%",
        data: categoryStats,
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: "rgba(0, 0, 0, 0.5)"
          }
        }
      }
    ]
  };

  myChart.setOption(option);
};

//*******************************************************************************

const statusChartRef = ref(null); // 修改为 statusChartRef 避免命名冲突

// **获取订单数据**
const fetchOrderStatusData = async () => {
  try {
    const response = await listEmergencyrescuerecords(); // 请求数据
    const orders = response.rows || []; // 确保数据存在
    const statusStats = processStatusData(orders);
    renderStatusChart(statusStats); // 渲染饼图
  } catch (error) {
    console.error("获取订单数据失败:", error);
  }
};

// **处理 `status` 的数量统计**
const processStatusData = (orders) => {
  const stats = { 1: 0, 2: 0 }; // 统计 `status` 为 1 和 2 的数量

  orders.forEach(order => {
    const status = order.status;
    if (stats[status] !== undefined) {
      stats[status]++;
    }
  });

  // 返回用于绘制饼图的数据
  return [
    { name: "已救援", value: stats[1] },  // 状态 1 对应 "已救援"
    { name: "未救援", value: stats[2] }   // 状态 2 对应 "未救援"
  ];
};

// **渲染 ECharts 饼状图**
const renderStatusChart = (statusStats) => {
  if (!statusChartRef.value) return;

  const myChart = echarts.init(statusChartRef.value);

  const option = {
    title: { text: "救援任务统计", left: "center" },
    tooltip: { trigger: "item" },
    legend: { orient: "vertical", left: "left" },
    series: [
      {
        name: "救援任务统计",
        type: "pie",
        radius: "50%",
        data: statusStats,
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: "rgba(0, 0, 0, 0.5)"
          }
        }
      }
    ]
  };

  myChart.setOption(option);
};




// **页面加载时获取数据**
onMounted(() => {
  fetchOrders();
  fetchOrderStatusData();
});





const version = ref('3.6.5')

function goTarget(url) {
  window.open(url, '__blank')
}
</script>

<style scoped lang="scss">
.home {
  blockquote {
    padding: 10px 20px;
    margin: 0 0 20px;
    font-size: 17.5px;
    border-left: 5px solid #eee;
  }
  hr {
    margin-top: 20px;
    margin-bottom: 20px;
    border: 0;
    border-top: 1px solid #eee;
  }
  .col-item {
    margin-bottom: 20px;
  }

  ul {
    padding: 0;
    margin: 0;
  }

  font-family: "open sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
  font-size: 13px;
  color: #676a6c;
  overflow-x: hidden;

  ul {
    list-style-type: none;
  }

  h4 {
    margin-top: 0px;
  }

  h2 {
    margin-top: 10px;
    font-size: 26px;
    font-weight: 100;
  }

  p {
    margin-top: 10px;

    b {
      font-weight: 700;
    }
  }

  .update-log {
    ol {
      display: block;
      list-style-type: decimal;
      margin-block-start: 1em;
      margin-block-end: 1em;
      margin-inline-start: 0;
      margin-inline-end: 0;
      padding-inline-start: 40px;
    }
  }
}
</style>

